package remote.controls
{
	
	/**
	 * 单选按钮组
	 * @createDate 2011-3-2
	 * @author 徐俊杰
	 */
	public class RadioButtonGroup
	{
		private var _radioButtons:Vector.<RadioButton>;
		private var _selection:RadioButton;
		
		
		public function RadioButtonGroup()
		{
			_radioButtons = new Vector.<RadioButton>();
		}
		
		public function addRadioButton(rb:RadioButton):void
		{
			_radioButtons.push(rb);
			rb.setGroup(this);
			if(_selection != null && rb.selected)
				rb.setSelected(false);
		}
		
		public function removeRadioButton(rb:RadioButton):void
		{
			var index:int = _radioButtons.indexOf(rb);
			if(index != -1)
			{
				_radioButtons.splice(index, 1);
				rb.setGroup(null);
				if(rb == selection)
				{
					_selection = null;
				}
			}
		}
		
		public function changeSelection(index:int):void
		{
			if(getRadioButtonAt(index) != null)
			{
				if(this._selection != null)
				{
					this._selection.setSelected(false);
				}
				this._selection = getRadioButtonAt(index);
				getRadioButtonAt(index).setSelected(true);
			}
		}
		
		public function getRadioButtonAt(index:int):RadioButton
		{
			return _radioButtons[index] 
		}
		
		public function get selection():RadioButton
		{
			return _selection;
		}
		
		public function set selection(value:RadioButton):void
		{
			var index:int = this._radioButtons.indexOf(value);
			if(index != -1)
			{
				this.changeSelection(index);	
			}
		}
		
		public function get selectedValue():Object
		{
			return _selection != null? _selection.value : null;
		}
		
		public function get numRadioButtons():int
		{
			return _radioButtons.length;
		}
	}
} 